package cranim.simulation;

import cranim.Constants;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Benedikt
 */
public class SimCase implements Constants{
    
    private ArrayList<RoundResult> rounds;
    private String attackerName, attackerCoords;
    private String defenderName, defenderCoords;
    private String time;
    
    public SimCase(Player attacker, Player defender){
        attackerName = attacker.getName();
        attackerCoords = attacker.getCoords();
        defenderName = defender.getName();
        defenderCoords = defender.getCoords();
        rounds = new ArrayList<>();
    }
    
    public void addRound(RoundResult round){
        rounds.add(round);
    }
    
    public RoundResult getRound(int roundnum){
        return rounds.get(roundnum);
    }
    
    public ArrayList<RoundResult> getRounds(){
        return rounds;
    }
    
    public int[] getShotsFired(){
        int[] shots = {0, 0};
        for(int i = 0; i < rounds.size(); i++){
            shots[ATTACKER]+= rounds.get(i).getAttackerShotCount()[0];
            shots[DEFENDER]+= rounds.get(i).getDefenderShotCount()[0];
        }
        return shots;
    }
    
    public int[] getHullDamgeDone(){
        int[] shots = {0, 0};
        for(int i = 0; i < rounds.size(); i++){
            shots[ATTACKER]+= rounds.get(i).getAttackerShotCount()[2];
            shots[DEFENDER]+= rounds.get(i).getDefenderShotCount()[2];
        }
        return shots;
    }
        
    public int getPlanetTemperature(){
        String planetPositionPattern = ".*:(\\d+)].*";
        Pattern pattern = Pattern.compile(planetPositionPattern, java.util.regex.Pattern.DOTALL+java.util.regex.Pattern.CANON_EQ);
        Matcher matcher = pattern.matcher(defenderCoords);
        if (matcher.matches()){
            matcher.groupCount();
            String planetPositionS = matcher.group(1);
            int planetPosition = Integer.valueOf(planetPositionS);
            if(planetPosition <= HOT_REGION){
                return HOT;
            }
            else if(planetPosition <= MEDIUM_REGION){
                return MEDIUM;
            }
            else return COLD;
        }
        else return -1;
    }
}
